<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('GFAPI')) {
    function gf_ml_admin_notice_gf_needed()
    {
        deactivate_plugins(plugin_basename(__file__));
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo esc_html(__p('GravityForms is not Installed please')); ?>
                <a href="https://github.com/wp-premium/gravityforms/tags"><?php echo esc_html(__p("download and install it")) ?></a>
            </p>
        </div>
        <?php
    }

    add_action('admin_notices', 'gf_ml_admin_notice_gf_needed');
}


if (!defined('gfvcard_dir')) {
    define('gfvcard_dir', get_stylesheet_directory());
}

if (!defined('vcard_static')) {
    define('vcard_static', get_stylesheet_directory_uri() . '/static/');
}

if (!function_exists('err_log')) {
    function err_log($obj)
    {
        error_log(
            print_r($obj, true)
        );
    }
}


function social_id_check()
{
    if (isset($_POST['social_id'])) {

        if (isset($_POST['phone'])) {
            require_once(gfvcard_dir . '/configs.php');

            $social_id = sanitize_text_field($_POST['social_id']);
            $phone = sanitize_text_field($_POST['phone']);
            $search_criteria = array('status' => 'active');
            $search_criteria['field_filters'][] = array(
                'key' => $social_id_field_id,
                'value' => $social_id);//        $entity=GFAPI::get_entries();
            $search_criteria['field_filters'][] = array(
                'key' => $phone_number_field_id,
                'value' => $phone);

            $entry = GFAPI::get_entries($form_id, $search_criteria);
            err_log($entry[0]['6.3']);
            if (sizeof($entry) == 0) {
                $response = array('found' => false);

            }
            else {
                err_log($entry[0][12]);
                $response = array('found' => true);
                $response['fname'] = $entry[0][$first_name_field_id];
                $response['lname'] = $entry[0][$last_name_field_id];
                $response['phone'] = $entry[0][$phone_number_field_id];
                $response['socialid'] = $entry[0][$social_id_field_id];
                $response['photo'] = $entry[0][$photo_field_id];
                $response['location'] = $entry[0][$location_field_id];
                $response['gender'] = $entry[0][$gender_field_id];

            } }

            else {
            require_once(gfvcard_dir . '/configs.php');

            $social_id = sanitize_text_field($_POST['social_id']);
            $search_criteria = array('status' => 'active');
            $search_criteria['field_filters'][] = array(
                'key' => $social_id_field_id,
                'value' => $social_id);//        $entity=GFAPI::get_entries();
            $entry = GFAPI::get_entries($form_id, $search_criteria);
            if (sizeof($entry) == 0) {
                $response = array('found' => false);

            } else {
                err_log($entry[0][12]);
                $response = array('found' => true);
                $response['phone'] = substr($entry[0][$phone_number_field_id], 0, 9);

            }

        }
        header("Content-Type: application/json");

        wp_send_json($response);
        //Don't forget to always exit in the ajax function.
        die();
    }
}



//function vue_load_scripts()
//{
//
//    wp_enqueue_script('vue-script', vcard_static . 'js/gravityvcard.js');
//    wp_localize_script('vue-script', 'vue_script_vars', array(
//            'ajaxurl' => admin_url( 'admin-ajax.php'),
//            'alert' => __('Hey! You have clicked the button!', 'pippin'),
//            'message' => __('You have clicked the other button. Good job!', 'pippin')
//        )
//    );
//
//}
//
//add_action('wp_enqueue_scripts', 'vue_load_scripts');



//add_action('wp_ajax_social_id_check', 'social_id_check');
add_action('wp_ajax_nopriv_social_id_check', 'social_id_check');
add_action('wp_ajax_social_id_check', 'social_id_check');
?>